home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / TASK_MAN / TASK_MAN.TXT < prev    next >
Text File  |  1990-09-07  |  10KB  |  197 lines

  1. Notes on the Task Manager
  2.  
  3.  
  4. This software source package is Copyright ⌐ 1990 by MPH╩╤╩All Rights Reserved. This package is hereby placed in the public domain. It may be freely distributed in source or object code format; however, the source code may not be sold for profit or charged for in any way. The source code must be distributed as a package including all H files, sample code and projects and this documentation.
  5.  
  6. The Task Manager is a package for creating and managing tasks╩╤╩separate execution threads that run non-preemptively in the background. Tasks should have low stack requirements, and should periodically call a Task Manager idling routine to allow other tasks to run. Tasks are ideal for lengthy processes that you would like to have run in the background, since the task runs in a separate execution thread from your event loop.
  7.  
  8. The Task Manager was written using THINK C 4.0. It requires the ANSI and MacTraps libraries, and the Gestalt function.
  9. Overview
  10. The Event Loop
  11. A simplified Macintosh event loop looks something like this:
  12.  
  13. void EventLoop( void )
  14. {
  15.     Boolean        gotEvent;
  16.     RgnHandle        cursorRgn;
  17.  
  18.  
  19.     cursorRgn = NewRgn();
  20.     do {
  21.         gotEvent = WaitNextEvent( everyEvent, &theEvent,                     GetSleep(), cursorRgn );
  22.         AdjustCursor( theEvent.where, cursorRgn );
  23.         if( gotEvent )
  24.             DoEvent( &theEvent );
  25.         else
  26.             DoIdle();
  27.     } while( TRUE );
  28. }
  29.  
  30. The application calls WaitNextEvent to retrieve the next event from the event queue. The application-defined routine GetSleep determines the minimum number of ticks to allow the operating system to wait before returning control to this application. When a null event occurs, the application calls its DoIdle routine to perform background operations. This is when the task manager should be called to run background tasks:
  31.  
  32. void DoIdle( void )
  33. {
  34.     RunTasks( GetWake());
  35. }
  36.  
  37. At idle time, the application calls the Task Manager routine RunTasks to process background tasks. The single parameter to RunTasks is the number of minimum number of ticks that background tasks may use. This time is calculated by the application-defined routine, GetWake.
  38. The Task
  39. The task itself is an application-defined routine that may look something like this:
  40.  
  41. void MyTaskProc( long taskRefCon )
  42. {
  43.     Boolean        moreToDo;
  44.  
  45.  
  46.     moreToDo = TRUE;
  47.     while( moreToDo ) {
  48.  
  49.         /* Allow other tasks to run */
  50.         TaskIdle();
  51.  
  52.         /* Do my task processing */
  53.         ╔
  54.     }
  55. }
  56.  
  57. The task procedure should periodically call the Task Manager routine TaskIdle, which allows the Task Manager to run other tasks. When TaskIdle is called, the Task Manager checks whether the number of ticks specified in the call to RunTasks has been exceeded, or if there are any events pending for the application. If there is no reason to suspend the task, then TaskIdle simply returns. Otherwise, the task is suspended and control returns to DoIdle, immediately after the call to RunTasks. The application can then run its event loop. The next time the application calls RunTasks, control will return to MyTaskProc immediately following the call to TaskIdle.
  58. Sleep and Wake Times
  59. The application spends its time alternating between periods of sleep and wake. It does its work while awake, and other applications do their work while it is asleep. You must choose a good balance of sleep and wake times so that your application can get its work done without too much expense from other applications.
  60.  
  61. There are two factors that should affect your application╒s sleep-wake cycle: whether the application is in the foreground or background, and whether the application has work to do at the moment. This table shows the four resulting situations and gives sample values for wake and sleep (all values are in ticks): 
  62.  
  63.     in Foreground
  64.     in Background
  65.  
  66. has Tasks    high wake (60)
  67. low sleep (0)    low wake (30)
  68. medium sleep (30)
  69. no Tasks    high wake (60)
  70. high sleep (12000)    low wake (30)
  71. high sleep (12000)
  72.  
  73. There are a few things to note here:
  74.  
  75. Ñ    When there are no tasks, we sleep for a ridiculously long time. The operating system will wake us when we receive an event. Otherwise, there╒s nothing to do, so we let other applications run.
  76.  
  77. Ñ    However, there╒s no need to adjust our wake time based on number of tasks, since RunTasks will return immediately when there are no tasks.
  78.  
  79. Ñ    We presume that the user wants the foreground application to run a little faster than applications in the background. When we are in the foreground and busy, we work for one second, then poll for events and allow other applications to work briefly.
  80. Task Manager Routines
  81. Task Manager Initialization
  82. Before using the Task Manager routines, your application should call InitTasking to set up the Task Manager╒s data structures:
  83.  
  84. OSErr InitTasking( void );
  85.  
  86. If the Task Manager could not be initialized due to memory limitations, InitTasking will return the appropriate system error code.
  87. Creating Tasks
  88. To create a new task, call NewTask:
  89.  
  90. OSErr NewTask( TaskProcPtr taskProc, long taskRefCon, int *taskRefNum );
  91.  
  92.     where
  93. taskProc    is the task procedure
  94. taskRefCon    is a parameter that is passed to the task
  95. taskRefNum    is the task╒s reference number. You may pass a NIL pointer if you don╒t need the reference number.
  96.  
  97. NewTask returns a Memory Manager error code if there is not enough memory available for the task╒s data structures or stack. The task╒s stack will be allocated from temporary memory if it is available.
  98.  
  99. A TaskProcPtr is defined as:
  100.  
  101. typedef void TaskProc( long taskRefCon );
  102. typedef TaskProc *TaskProcPtr;
  103.  
  104. The task procedure can either be a static or an extern function, but it╒s CODE segment should remain loaded until the task procedure returns.
  105. Disposing of Tasks
  106. When a task has finished it╒s work, its task procedure simply returns. It will be automatically removed. To dispose of a task before its task procedure returns (for example, if the user selects ╥Quit╙), call DisposeTask:
  107.  
  108. OSErr DisposeTask( int taskRefNum );
  109.  
  110.     where
  111. taskRefNum    is the task╒s reference number.
  112.  
  113. DisposeTask returns an error code (paramErr) if the task reference number is invalid.
  114. Managing Task Stacks
  115. A task╒s stack size is 8K by default. You can create a task with a different stack size by calling SetStackSize before calling NewTask. The format for SetStackSize is:
  116.  
  117. Size SetStackSize( Size stackSize );
  118.  
  119.     where
  120. stackSize    is the size that you want NewTask to use when allocating stacks.
  121.  
  122. SetStackSize returns the previous stack size. You can use the constant DefaultStackSize to refer to the Task Manager╒s stack size default.
  123.  
  124. To help determine how large a stack should be, you can call SetStackChecking after you call InitTasking. If stack checking is enabled, then the Task Manager will break to the debugger when a task procedure returns, and it will indicate how much stack space was actually used. The format for SetStackChecking is:
  125.  
  126. void SetStackChecking( Boolean checking );
  127.  
  128.     where
  129. checking    is TRUE if you want to enable stack checking, and FALSE if you want to disable stack checking.
  130.  
  131. The Task Manager will also break to the debugger if it detects that a task╒s stack has been overrun.
  132.  
  133. Within your task procedure, you may wish to call the Memory Manager routine StackSpace to determine how much room is available on your stack. You can use this information to determine how deeply you wish to nest a recursive call, for example.
  134. Letting Tasks Run
  135. Your application should call RunTasks in response to a null event or an error return code from GetNextEvent or WaitNextEvent:
  136.  
  137. void RunTasks( unsigned long wakeTime );
  138.  
  139.     where
  140. wakeTime    is the amount of time you want allocated to background tasks.
  141.  
  142. The constant DefaultWakeTime is a good choice for the wakeTime parameter.
  143.  
  144. Occasionally, your task procedure should call TaskIdle so that it may allow other applications and other tasks in your application to run.
  145.  
  146. void TaskIdle( void );
  147.  
  148. TaskIdle will return control to the application (that is, the application will regain control after it╒s call to RunTasks) if there are no more tasks to run, if the wake time specified in RunTasks has elapsed, or if there is an event pending for the application.
  149. Keeping Track of Tasks 
  150. Your application can keep track of its tasks by calling these Task Manager routines:
  151.  
  152.  
  153. int CountTasks( void );
  154.  
  155. CountTasks returns the number of tasks that the Task Manager is currently running.
  156.  
  157.  
  158. int CurrentTask( void );
  159.  
  160. CurrentTask returns the task reference number of the current task, or 0 if there is no current task (if called from the application, for example).
  161.  
  162.  
  163. int GetIndTask( int index );
  164.  
  165.     where
  166. index    is the number of the nth task counting from 0 to CountTasks() - 1
  167.  
  168. GetIndTask returns the task reference number of the given task, or 0 if index is invalid.
  169.  
  170.  
  171. long GetTaskRefCon( int taskRefNum );
  172.  
  173.     where
  174. taskRefNum    is the reference number of the task whose reference constant you want
  175.  
  176. GetTaskRefCon returns the reference constant for the given task. This is useful if you want to send messages to your tasks by changing parameters that are pointed to by the tasks╒ reference constants.
  177. Limitations
  178. Task Procedures
  179. You are only limited by memory to the number of tasks you can create simultaneously.
  180.  
  181. Task procedures can call any Toolbox routine (including QuickDraw). However, the current grafPort is not preserved across calls to TaskIdle. If you have several tasks drawing to different ports, or if you draw in your application code, you may need to call SetPort after calling TaskIdle.
  182. ANSI
  183. The Task Manager uses the setjmp and longjmp routines from the THINK C ANSI library. If you don╒t want to include ANSI, then include the file setjmp.c in your project.
  184. Profiler
  185. Task.c will not work if it is compiled with the Profiler code generation option. To circumvent this, create a project (Task.╣) containing only Task.c. Turn the Profiler code generation option off and compile Task.c. In your application╒s project, remove Task.c and replace it with Task.╣.
  186.  
  187. You should also avoid profiling across TaskIdle calls. Set the global variable _profile to FALSE before calling TaskIdle, then set it back to TRUE afterwards.
  188. Yours Truly
  189. I welcome any comments or suggestions that will help me improve or extend the functionality of the Task Manager. You can reach me at:
  190.  
  191.     AppleLink:        SAS.HECHT
  192.     Internet:        hecht@unx.sas.com
  193.  
  194. Happy Tasking!
  195.  
  196. ╤ Michael Hecht
  197.